﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DRHotel_v1.Classes;

namespace DRHotel_v1
{
    public partial class Login : Form
    {
        Account ac = new Account();
        LoginDBManager dbm = new LoginDBManager();
        DataSet ds = new DataSet();
        int i = 0;

        public Login()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (tbxUsername.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Empty Username!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (tbxPwd.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Empty Password!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }


            if (LoginDBManager.Login(tbxUsername.Text, tbxPwd.Text))
            {
                MainForm fm = new MainForm();
                ac = LoginDBManager.GetAccountInfo(tbxUsername.Text);
                fm.name = ac.username;
                fm.pwd = ac.password;
                fm.type = ac.userType;
                fm.staffID = ac.staffID.ToString();
                fm.RefToLogIn = this;
                this.Hide();
                refreshTbxes();
                fm.Show();
            }
            else
            {
                MessageBox.Show("Login Failed!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                i++;
                if (i == 3)
                {
                    MessageBox.Show("Login failed 3 times!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.Close();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Confirm Cancel?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        public void refreshTbxes()
        {
            tbxPwd.Clear();
            tbxUsername.Clear();
        }
    }
}
